global class LeadSync_AsyncApex {
    public static boolean firstTrigger = true;
    public static boolean firstCount = true;
    
    @future
    public static void SyncLeadInfo(Set<Id> Leads){
        firstTrigger = false;
        // Fields to Update: Account Status, Status Explanation, Company Name, CRM System, Salesforce Verified
        System.Debug('=== Syncing Lead Info with Accounts and Other Leads ===');
        // Get the data from the Sources, create a map by Account ID
        Map<Id,Lead> NewValues = new Map<Id,Lead>();
        for(Lead l : [select Account__c,Status,Status_Explanation__c, Company, CRM_System__c, Salesforce_Verified__c from Lead where Id in :Leads and IsConverted = False]){
            NewValues.put(l.Account__c,l);
        }
        
        // Query the Accounts
        List<Account> Accounts = [select Status__c, Status_Explanation__c, Name, CRM_System__c, Salesforce_Verified__c from Account where Id in :NewValues.keySet() and Name not in ('salesforce.com')];
        
        // Update the Accounts
        for(Account a : Accounts){
            a.Status__c = NewValues.get(a.Id).Status;
            a.Status_Explanation__c = NewValues.get(a.Id).Status_Explanation__c;
            a.Name = NewValues.get(a.Id).Company;
            a.CRM_System__C = NewValues.get(a.Id).CRM_System__c;
            a.Salesforce_Verified__c = NewValues.get(a.Id).Salesforce_Verified__c;
            System.Debug('Account Updated with: '+a);
        }
        update Accounts;
        
        
        // Query the Lead coworkers
        List<Lead> UpdatedLeads = [select Status,Status_Explanation__c, Company, CRM_System__c, Salesforce_Verified__c, Account__c from Lead where Id not in :Leads AND Account__c in :NewValues.keySet() and IsConverted = False];
        
        // Update the Lead coworkers
        if(UpdatedLeads.size()>0){
            for(Lead l : UpdatedLeads){
                l.Status = NewValues.get(l.Account__c).Status;
                l.Status_Explanation__c = NewValues.get(l.Account__c).Status_Explanation__c;
                l.Company = NewValues.get(l.Account__c).Company;
                l.CRM_System__c = NewValues.get(l.Account__c).CRM_System__c;
                l.Salesforce_Verified__c = NewValues.get(l.Account__c).Salesforce_Verified__c;
            }
            update UpdatedLeads;
        }
        
        // Query the original Leads
        List<Lead> OriginalLeads = [select Account__c,Status,Status_Explanation__c, Company from Lead where Id in :Leads AND Override_Account_Fields__c = true];
        if(OriginalLeads.size()>0){
            for(Lead l : OriginalLeads){
                l.Override_Account_Fields__c = false;
            }   
            update OriginalLeads;
        }
        
        
        System.Debug('=== Done Syncing Lead Info with Accounts and Other Leads ===');
        
    }
    
    @future
    public static void SyncAccountInfo(Set<Id> Accounts, Set<Id> AccountOwnerChanges){
        firstTrigger = false;
        // Fields to Update: Account Status, Status Explanation, Company Name, CRM System, Verified Salesforce By, Owner (if Account Owner changes),
        System.Debug('================> Entered Async Apex');
        // Get the data from the Sources, create a map by Account ID
        Map<Id,Account> NewValues = new Map<Id,Account>();
        for(Account a : [select Status__c, Status_Explanation__c, Name, OwnerId, CRM_System__c, Salesforce_Verified__c from Account where Id in :Accounts]){
            NewValues.put(a.Id,a);
        }
                    
        // Query the Leads
        List<Lead> Leads = [select Account__c, Status, Status_Explanation__c, Company, CRM_System__c, Salesforce_Verified__c, OwnerId from Lead where Account__c in :Accounts and IsConverted = False];
        
        // Update the Leads
        if(Leads.size()>0){
            System.Debug('================> We found '+Leads.size()+' Leads to update');
            for(Lead l : Leads){
                l.Status = NewValues.get(l.Account__c).Status__c;
                l.Status_Explanation__c = NewValues.get(l.Account__c).Status_Explanation__c;
                l.Company = NewValues.get(l.Account__c).Name;
                l.CRM_System__c = NewValues.get(l.Account__c).CRM_System__c;
                l.Salesforce_Verified__c = NewValues.get(l.Account__c).Salesforce_Verified__c;
                if(AccountOwnerChanges.contains(l.Account__c)){
                    l.OwnerId = NewValues.get(l.Account__c).OwnerId;
                }
            }
            update Leads;
            System.Debug('================> Done updating Leads');
        }
        
        
    }   
    
}